from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

op = webdriver.ChromeOptions()
op.add_experimental_option("excludeSwitches",["enable-automation"])
op.add_experimental_option("useAutomationExtension",False)
driver = webdriver.Chrome(options = op)

driver.get(r"http://news.baidu.com/")

newssearchtest = driver.find_element(By.CSS_SELECTOR, '#pane-news > div > ul > li.hdline1 > strong > a:nth-child(1)')
content = newssearchtest.text
url = newssearchtest.get_attribute("href")
print(url)
newssearchtest.click()
sleep(2)


current = driver.current_window_handle
all_hanle = driver.window_handles
print(current)
print("------------------")
print(all_hanle)
driver.switch_to.window(all_hanle[1])
print(driver.title)
sleep(2)
nowurl = driver.current_url
print(nowurl)

assert url in nowurl

driver.close()
driver.switch_to.window(all_hanle[0])
driver.find_element(By.CSS_SELECTOR,"#ww").send_keys("我是rapper")
driver.find_element(By.CSS_SELECTOR, "#sugarea > span.s_btn_wr").click()
sleep(2)
newssearchtest = driver.find_element(By.CSS_SELECTOR, "#kw").get_attribute('value')
assert '我是rapper' in newssearchtest
driver.quit()